import request from '@/utils/request'
import form from '@/utils/form'
import { cached_data } from '@/utils/cache'

const api = 'partyUser/'

/**
 * 获取党团列表
 *
 * @export
 * @param {*} { group, labor, typeInParty, company, page }
 * @return {*}
 */
export function getList({ group, labor, typeInParty, company, page }) {
  return new Promise((res, rej) => {
    request.post(`${api}list`, {
      group: form.toQueryValue(group),
      labor: form.toQueryValue(labor),
      typeInParty: form.toQueryValue(typeInParty),
      company: form.toQueryValue(company),
      page
    }).then(data => {
      if (data && data.list) {
        data.list.map(i => {
          i.zzmm = typeInPartyDict_reverse[i.typeInParty]
          i.name = i.userName
        })
      }
      res(data)
    }).catch(e => rej(e))
  })
}

/** @type {TypeInPartyEnum} */
const typeInPartyDict = {
  '无': 0,
  '群众': 1,
  '少先队员': 2,
  '团员': 4,
  '入党积极分子': 8,
  '预备党员': 10,
  '正式党员': 12
}
const typeInPartyDict_reverse = (() => {
  const d = typeInPartyDict
  const r = {}
  Object.keys(d).map(i => {
    r[d[i]] = i
  })
  return r
})()

/**
 * 提交党团
 *
 * @export
 * @param {*} { username, dilabo, appotime, post, ruwutime, rudtime, zzmm, dxznum, posichan, number
 * }
 * @return {*}
 */
export function putItem({ username, dilabo, appotime, post, ruwutime, rudtime, zzmm, dxznum, posichan, number }) {
  return request.put(`${api}/info`, {
    username,
    dilabo,
    appotime,
    post,
    ruwutime,
    rudtime,
    zzmm: typeInPartyDict[zzmm] || 0,
    dxznum,
    posichan,
    number
  })
}

/**
 * 编辑党团
 *
 * @export
 * @param {*} { id, username, dilabo, appotime, post, ruwutime, rudtime, zzmm, dxznum, posichan, number }
 * @return {*}
 */
export function patchItem({ id, username, dilabo, appotime, post, ruwutime, rudtime, zzmm, dxznum, posichan, number }) {
  return request.patch(`${api}/info`, {
    id,
    username,
    dilabo,
    appotime,
    post,
    ruwutime,
    rudtime,
    zzmm: typeInPartyDict[zzmm] || 0,
    dxznum,
    posichan,
    number
  })
}

/**
 * 移除党团
 *
 * @export
 * @param {*} { username }
 * @return {*}
 */
export function removeInfo({ username }) {
  return request.delete(`${api}/info`, {
    params: {
      userid: username
    }
  })
}

const urlGetTypeInParty = `${api}/typesInParty`
/**
 * 获取政治面貌列表
 *
 * @export
 */
export function typeInParty() {
  return new Promise((res, rej) => {
    cached_data(`${urlGetTypeInParty}`, () =>
      request.get(urlGetTypeInParty)
    ).then(data => {
      data.list.map(i => {
        i.data = i.alias
      })
      res(data.list)
    }).catch(e => rej(e))
  })
}
const urlGetPartyDuties = `${api}/partyDuties`
/**
 * 获取党内职务列表
 *
 * @export
 */
export function partyDuties() {
  return new Promise((res, rej) => {
    cached_data(`${urlGetPartyDuties}`, () =>
      request.get(urlGetPartyDuties)
    ).then(data => {
      data.list.map(i => {
        i.value = i.name
      })
      res(data.list)
    }).catch(e => rej(e))
  })
}
export default {
  getList,
  putItem,
  patchItem,
  removeInfo,
  typeInParty,
  partyDuties
}
